package com.shujia.thread_local;

public class StudentName {

    public static ThreadLocal<String> tl = new ThreadLocal<>();
    public static ThreadLocal<String> tl2 = new ThreadLocal<>();


    public void eat() {
        System.out.println("eat: " + Thread.currentThread().getName() + ":" + StudentName.tl.get());
        System.out.println("eat: " + Thread.currentThread().getName() + ":" + StudentName.tl2.get());

    }

    public static void main(String[] args) {
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                // 设置当前线程的局部变量
                StudentName.tl.set("晓明");
                StudentName.tl2.set("晓明2");

                System.out.println(Thread.currentThread().getName() + ":" + StudentName.tl.get());

                StudentName sn = new StudentName();
                sn.eat();
            }
        }, "线程1");

        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                // 设置当前线程的局部变量
                StudentName.tl.set("小红");

                System.out.println(Thread.currentThread().getName() + ":" + StudentName.tl.get());

                StudentName sn = new StudentName();
                sn.eat();
            }
        }, "线程2");

        t1.start();
        t2.start();
    }
}
